<template>
  <div>
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in list"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="small"
          @click="addSubmit"
        >确定</el-button>
        <el-button size="small" @click="offw">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getHeadInfo, addDepartments, EditDepartments, EditDepartmentsList } from '@/api/department'
export default {
  props: {
    pid: {
      type: String,
      required: true
    },
    isEditor: {
      type: Boolean,
      required: true
    },
    upDateList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 自定义校验部门名称
    const validateName = (rule, value, callback) => {
      let nameList = this.upDateList.map(item => item.name)
      // 编辑(筛选出同级和子级)
      if (this.isEditor) {
        const res = this.upDateList.find(item => item.id === this.pid)
        const truePid = res.pid
        nameList = this.upDateList.filter(item => item.id !== this.pid && item.pid === truePid).map(item => item.name)
      }
      // 添加(筛选出子级)
      nameList = this.upDateList.filter(item => item.pid === this.pid).map(item => item.name)

      if (nameList.includes(value)) {
        callback(new Error('部门名称被占用'))
      } else {
        callback()
      }
    }
    // 自定义校验部门编码
    const validateCode = (rule, value, callback) => {
      let codeList = this.upDateList.map(item => item.code)
      // 编辑(筛选出同级和子级)
      if (this.isEditor) {
        const res = this.upDateList.find(item => item.id === this.pid)
        const truePid = res.pid
        codeList = this.upDateList.filter(item => item.id !== this.pid && item.pid === truePid).map(item => item.code)
      }
      // 添加(筛选出子级)
      codeList = this.upDateList.filter(item => item.pid === this.pid).map(item => item.code)

      if (codeList.includes(value)) {
        callback(new Error('部门编号被占用'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      list: []
    }
  },
  created() {
    this.loadHeadInfo()
    this.loadDepartments()
  },
  methods: {
    // 获取部门负责人
    async loadHeadInfo() {
      const res = await getHeadInfo()
      this.list = res.data
    },
    // 添加|编辑部门确认按钮
    addSubmit() {
      //  兜底校验
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.isEditor) {
            EditDepartmentsList(this.form)
            this.$emit('offWindows')
          } else {
            addDepartments({ ...this.form, pid: this.pid })
            this.$emit('offWindows')
          }
        } else {
          this.$message.error('校验未通过')
        }
      })
    },
    async loadDepartments() {
      if (this.isEditor) {
        const res = await EditDepartments(this.pid)
        this.form = res.data
      }
    },
    // 编辑部门弹窗取消
    offw() {
      this.$emit('offWindows')
    }
  }
}
</script>
